#!/usr/bin/perl
#network-lib.pl

do '../web-lib.pl';
&init_config();

$net_scripts_dir = "/etc/sysconfig/network-scripts";

sub active_interfaces
{
local(@rv, @lines, $l);
#open(IFC, "ifconfig -a |");
open(IFC, "ifconfig |");
while(<IFC>) {
	s/\r|\n//g;
	if (/^\S+/) { push(@lines, $_); }
	else { $lines[$#lines] .= $_; }
	}
close(IFC);
foreach $l (@lines) {
	local %ifc;
	$l =~ /^([^:\s]+)/; $ifc{'name'} = $1;
	$l =~ /^(\S+)/; $ifc{'fullname'} = $1;
	if ($l =~ /bond0/) { $ifc{'virtual'} = "MASTER"; }
	if ($l =~ /inet addr:(\S+)/) { $ifc{'address'} = $1; }
	elsif (!$_[0]) { next; }
	if ($l =~ /Mask:(\S+)/) { $ifc{'netmask'} = $1; }
	if ($l =~ /Bcast:(\S+)/) { $ifc{'broadcast'} = $1; }
	if ($l =~ /HWaddr (\S+)/) { $ifc{'ether'} = $1; }
	if ($l =~ /(SLAVE)/) { $ifc{'virtual'} = $1; }
#	if ($l =~ /MTU:(\d+)/) { $ifc{'mtu'} = $1; }
	if ($l =~ /Interrupt:(\d+)/) { $ifc{'irq'} = $1; }
	if ($l =~ /Base address:(\S+)/) { $ifc{'ioaddr'} = $1; }
#	$ifc{'up'}++ if ($l =~ /\sUP\s/);
	if ($l =~ /\sUP\s/){$ifc{'up'} = "UP";}
	if ($l =~ /NOARP/){$ifc{'up'} = "BACKUP";}
	if ($l =~ /RX\sbytes:(\S+\s\S+\s\S+)/){$ifc{'rx'} = $1;}
	if ($l =~ /TX\sbytes:(\S+\s\S+\s\S+)/){$ifc{'tx'} = $1;}
	$ifc{'edit'} = ($ifc{'name'} !~ /^ppp/);
	#$ifc{'index'} = scalar(@rv);
	push(@rv, \%ifc);
	}
return @rv;
}

sub get_interface
{
	local (%conf, $b);
	&read_env_file("$net_scripts_dir/ifcfg-$_[0]", \%conf);
	$b->{'fullname'} = $conf{'DEVICE'};
	if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
		$b->{'name'} = $1;
		$b->{'virtual'} = $2;
	}
	else { $b->{'name'} = $b->{'fullname'}; }
	$b->{'up'} = ($conf{'ONBOOT'} eq 'yes');
	$b->{'address'} = $conf{'IPADDR'};
	$b->{'netmask'} = $conf{'NETMASK'};
	$b->{'broadcast'} = $conf{'BROADCAST'};
	$b->{'gateway'} = $conf{'GATEWAY'};
	$b->{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
	$b->{'bootp'} = ($conf{'BOOTPROTO'} eq 'bootp');
	$b->{'edit'} = ($b->{'name'} !~ /^ppp|irlan/);

	local $line;
	open(IFC, "ifconfig -a $_[0]|");
	while(<IFC>) {
		s/\r|\n//g;
		if (/^\S+/) { $line = $_; }
		else { $line .= $_; }
	}
	close(IFC);
	if ($line =~ /Device\snot\sfound/) { return; }
	if ($line =~ /HWaddr (\S+)/) { $b->{'ether'} = $1; }
	if ($line =~ /Interrupt:(\d+)/) { $b->{'irq'} = $1; }
	if ($line =~ /(SLAVE)/) { $b->{'virtual'} = $1; }
	if ($line =~ /(MASTER)/) { $b->{'virtual'} = $1; }
	if ($line =~ /RX\sbytes:(\S+\s\S+\s\S+)/) { $b->{'rx'} = $1; }
	if ($line =~ /TX\sbytes:(\S+\s\S+\s\S+)/) { $b->{'tx'} = $1; }
	if ($line =~ /bond0/) { $b->{'name'} = "bond0"; }
	
	return $b;
}

sub get_active_interface
{
	local (%conf, $b);
	&read_env_file("$net_scripts_dir/ifcfg-$_[0]", \%conf);
	$b->{'fullname'} = $conf{'DEVICE'};
	if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
		$b->{'name'} = $1;
		$b->{'virtual'} = $2;
	}
	else { $b->{'name'} = $b->{'fullname'}; }
	$b->{'up'} = ($conf{'ONBOOT'} eq 'yes');
#	$b->{'address'} = $conf{'IPADDR'};
#	$b->{'netmask'} = $conf{'NETMASK'};
#	$b->{'broadcast'} = $conf{'BROADCAST'};
	$b->{'gateway'} = $conf{'GATEWAY'};
	$b->{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
	$b->{'bootp'} = ($conf{'BOOTPROTO'} eq 'bootp');
	$b->{'edit'} = ($b->{'name'} !~ /^ppp|irlan/);

	local $line;
	open(IFC, "ifconfig -a $_[0]|");
	while(<IFC>) {
		s/\r|\n//g;
		if (/^\S+/) { $line = $_; }
		else { $line .= $_; }
	}
	close(IFC);
	if ($line =~ /Device\snot\sfound/) { return; }
	if ($line =~ /HWaddr (\S+)/) { $b->{'ether'} = $1; }
	if ($line =~ /inet addr:(\S+)/) { $b->{'address'} = $1; }
	if ($line =~ /Mask:(\S+)/) { $b->{'netmask'} = $1; }
	if ($line =~ /Bcast:(\S+)/) { $b->{'broadcast'} = $1; }
	if ($line =~ /Interrupt:(\d+)/) { $b->{'irq'} = $1; }
	if ($line =~ /(SLAVE)/) { $b->{'virtual'} = $1; }
	if ($line =~ /(MASTER)/) { $b->{'virtual'} = $1; }
	if ($line =~ /RX\sbytes:(\S+\s\S+\s\S+)/) { $b->{'rx'} = $1; }
	if ($line =~ /TX\sbytes:(\S+\s\S+\s\S+)/) { $b->{'tx'} = $1; }
	if ($line =~ /bond0/) { $b->{'name'} = "bond0"; }
	
	return $b;
}

sub save_interface
{
local(%conf);
#local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
#				       : $_[0]->{'name'};
local $name = $_[0]->{'name'};
&lock_file("$net_scripts_dir/ifcfg-$name");
&read_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
$conf{'DEVICE'} = $name;
$conf{'IPADDR'} = $_[0]->{'address'};
local($ip1, $ip2, $ip3, $ip4) = split(/\./, $_[0]->{'address'});
$conf{'NETMASK'} = $_[0]->{'netmask'};
local($nm1, $nm2, $nm3, $nm4) = split(/\./, $_[0]->{'netmask'});
if ($_[0]->{'address'} && $_[0]->{'netmask'}) {
	$conf{'NETWORK'} = sprintf "%d.%d.%d.%d",
				($ip1 & int($nm1))&0xff,
				($ip2 & int($nm2))&0xff,
				($ip3 & int($nm3))&0xff,
				($ip4 & int($nm4))&0xff;
	}
else {
	$conf{'NETWORK'} = '';
	}
$conf{'BROADCAST'} = $_[0]->{'broadcast'};
#$conf{'GATEWAY'} = $_[0]->{'gateway'};
$conf{'ONBOOT'} = $_[0]->{'up'} ? "yes" : "no";
$conf{'BOOTPROTO'} = $_[0]->{'bootp'} ? "bootp" :
		     $_[0]->{'dhcp'} ? "dhcp" : "static";
&write_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
&unlock_file("$net_scripts_dir/ifcfg-$name");
}
sub get_showname
{
	if($_[0] eq "bond0"){return $text{'bond0'};}
	elsif($_[0] eq "eth0"){return $text{'eth0'};}
	elsif($_[0] eq "eth1"){return $text{'eth1'};}
	elsif($_[0] eq "eth2"){return $text{'eth2'};}
	elsif($_[0] eq "eth3"){return $text{'eth3'};}
	elsif($_[0] eq "eth4"){return $text{'eth4'};}
	elsif($_[0] eq "eth5"){return $text{'eth5'};}
}
sub get_adaptername
{
	open (FIC, "</proc/pci");
	my $name, $temp;
	$name="Virtual NIC";
	while ($line = <FIC>) {
		if ($line =~ /Bus/) {
			$device=1;
			$temp = "";
			next;
		}
		if ($device) {
			if (($line =~ /Ethernet controller/))
			{
				$temp = (split (":", $line))[1];
				next;
			}
			if (( $line =~ /IRQ\s+(\d+)./)&&($temp))
			{
				if ($1 eq $_[0])
				{
					$name = $temp;
					break;
				}
				$device=0;
				next;
			}
			$device=0;
		}
	}
	close (FIC);
	return $name;
}

sub ifdown
{
	`$net_scripts_dir/ifdown $_[0]`;
}

sub ifup
{
	`$net_scripts_dir/ifup $_[0]`;
}

sub check_bond_status()
{
	&read_file_byline("/etc/bond.conf",\@lref);
	foreach $line (@lref)
	{
		if($line=~/TYPE:(\S+)/)
	        {
	        	$type = $1;
	        	last;
	        }
        }
        return $type;
}
sub check_adapter_status()
{
	my $bond_status = &check_bond_status();
	if($_[0] eq ""){return $text{'ifcs_down'};}
	if($_[0] eq "UP"){return $text{'ifcs_up'};}
	if(($_[0] eq "BACKUP")&&($bond_status ne "FT")){return $text{'ifcs_up'};}
	if(($_[0] eq "BACKUP")&&($bond_status eq "FT")){return $text{'ifcs_backup'};}
}
sub read_file_byline()
{
        my ($ptf, $filename);

        $filename = @_[0];
        $ptf = @_[1];

        open(RFILE, $filename);
        @$ptf = <RFILE>;
        close(RFILE);
}
sub check_ip_conflict()
{
#	print "$_[0],$_[1],$_[2],$_[3]";
	@ip1 = split(/\./, $_[0]);
#	print "@ip1";
#	print "@ip1,@mask1,@ip2,@mask2,$ip161,$mask161,$ip162,$mask162";
	@mask1 = split(/\./, $_[1]);
	@ip2 = split(/\./, $_[2]);
	@mask2 = split(/\./, $_[3]);
	$ip161 = (($ip1[0]*256+$ip1[1])*256+$ip1[2])*256+$ip1[3];
	$mask161 = (($mask1[0]*256+$mask1[1])*256+$mask1[2])*256+$mask1[3];
	$ip162 = (($ip2[0]*256+$ip2[1])*256+$ip2[2])*256+$ip2[3];
	$mask162 = (($mask2[0]*256+$mask2[1])*256+$mask2[2])*256+$mask2[3];
#	print "@ip1,@mask1,@ip2,@mask2,$ip161,$mask161,$ip162,$mask162";
	if(($ip161&$mask161) == ($ip162&$mask162))
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
sub calc_broadcast()
{
	@ip = split(/\./, $_[0]);
	@mask = split(/\./, $_[1]);
	local @gateway;
	my $i;
	for($i=0;$i<@ip;$i++)
	{
		$gateway[$i]=$ip[$i]|(255-$mask[$i]);
	}
	$gw = join '.',@gateway;
	return $gw;
}
sub change_gatewaydev()
{
 	&lock_file("/etc/sysconfig/network");
 	&read_env_file("/etc/sysconfig/network", \%conf);
 	$conf{'GATEWAYDEV'} = $_[0];
 	&write_env_file("/etc/sysconfig/network", \%conf);
 	&unlock_file("/etc/sysconfig/network");
}

1;
